<?php
//require_once 'func.php';
require_once './lib/ReadTitle.php';
$read = new ReadTitle();

$uploadPath = dirname(__FILE__) . '/uploads/';

$isPost = strtolower($_SERVER['REQUEST_METHOD']) == 'post';
if ($isPost) {
    if (isset($_POST['translate'])) {
        foreach ($_FILES['translate']['error'] as $i => $error) {
            if (0 === $error) {
                $file = $uploadPath . $_POST['translate_file'][$i];
                move_uploaded_file($_FILES['translate']['tmp_name'][$i], $file);
            }
        }
    } elseif (isset($_POST['subtitle'])) {
        $file = $uploadPath . $_FILES['upload']['name'];
        move_uploaded_file($_FILES['upload']['tmp_name'], $file);
        $read->setFilename($file)
            ->writeFileWords();
    } elseif (isset($_POST['merge'])) {
        $file = $_POST['merge_file'];
        $read->setFilename($uploadPath . $file)
            ->writeCompare();
    }
}

$dh  = opendir($uploadPath);
$files = array();
$filesAdd = array();
while (false !== ($filename = readdir($dh))) {
    if ($filename == '.' || $filename == '..') {
        continue;
    }
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    $additionalExt = substr($filename, -10, 10);
    $name = pathinfo($filename, PATHINFO_BASENAME);
    $additionsList = array('_words.txt', '_trans.txt', '_merge.txt');
    if (in_array($ext, array('srt', 'stp', 'sub'))) {
        $files[] = $filename;
    } elseif (false !== ($key = in_array($additionalExt, $additionsList))) {
        $name = substr($filename, 0, -10);
        $filesAdd[$name][$additionsList[$key]] = $filename;
    }
}
if ($files) {
    sort($files);
}

require_once '_start.php';
?>
<div class="container_12">
    <form enctype="multipart/form-data" action="index.php" method="post">
        <fieldset>
            <legend>Send subtitle</legend>
            <input name="upload" type="file">
            <input type="submit" name="subtitle" value="Send File">
        </fieldset>
        <?php if ($files) : ?>
        <fieldset>
            <legend>Or perform exist file</legend>
            <?php foreach ($files as $i => $file) : ?>
            <?php $name = pathinfo($file, PATHINFO_FILENAME); ?>
            <div class="<?php echo $i % 2 ? '' : 'odd' ?>" >
                <h3><?php echo $name ?></h3>

                <?php if (file_exists($uploadPath . $name . '_words.txt')) : ?>
                <div>
                    <strong>Words</strong>
                    <a href="uploads/<?php echo $name?>_words.txt">Download words</a> <br>
                    <input type="file" name="translate[]" value="<?php echo $file?>" />
                    <input type="submit" name="translate"  value="Upload file with translated words">
                    <input type="hidden" name="translate_file[]"  value="<?php echo $name . '_trans.txt' ?>">
                </div>
                <?php endif; ?>
                <?php if (file_exists($uploadPath . $name . '_trans.txt')) : ?>
                <div>
                    <strong>Translated words</strong>
                    <a href="uploads/<?php echo $name?>_trans.txt">Download translate</a><br>
                </div>
                <?php endif; ?>
                <div>
                    <strong>Words merged with translate</strong>
                    <input id="merge_<?php echo $i ?>" type="hidden" value="<?php echo $file ?>">
                    <input type="submit" name="merge" value="Merge" onclick="document.getElementById('merge_<?php echo $i ?>').name='merge_file'">
                    <?php if (file_exists($uploadPath . $name . '_merge.txt')) : ?>
                        OR <a href="uploads/<?php echo $name?>_merge.txt">Download merged</a>
                    <?php endif; ?>
                </div>
            </div>
            <?php endforeach; ?>
        </fieldset>
        <?php endif; ?>
    </form>
</div>

<?php require_once '_end.php'; ?>
